﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.InvDoc.Enums;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForTransferFormUpdate : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //料品形态转换单
            UFIDA.U9.InvDoc.TransferForm.TransferForm tran = key.GetEntity() as UFIDA.U9.InvDoc.TransferForm.TransferForm;
            if (tran == null) return;

            #region 料品形态转换前转换后数量相等
            if (tran.OriginalData.Status == INVDocStatus.Open && tran.Status==INVDocStatus.Approving)
            {
                foreach (var line in tran.TransferFormLs)
                {
                    String sql = $@"select line.StoreUOMQty qty from InvDoc_TransferForm tra
                                    left join InvDoc_TransferFormL line on line.TransferForm = tra.id
                                    where line.id = '{line.ID}' ";
                    DataSet ds = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                    if(ds == null || ds.Tables == null || ds.Tables.Count == 0 || ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count == 0 || ds.Tables[0].Rows[0] == null)
                    {
                        throw new Exception($"行号:'[{line.DocLineNo}]',请填写数量");
                    }
                    String sql1 = $@"select sum(sbL.StoreUOMQty) Qty from InvDoc_TransferForm tra
                                        left join InvDoc_TransferFormL line on line.TransferForm = tra.id
                                         left join InvDoc_TransferFormSL sbL on sbL.TransferFormL  = line.ID 
                                         where line.id = '{line.ID}'
                                         group by sbL.TransferFormL";
                    DataSet ds1 = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);

                    if (ds1 == null || ds1.Tables == null || ds1.Tables.Count == 0 || ds1.Tables[0].Rows == null || ds1.Tables[0].Rows.Count == 0 || ds1.Tables[0].Rows[0] == null)
                    {
                        throw new Exception($"行号:'[{line.DocLineNo}]',请填写转换后数量");
                    }
                    if (ds.Tables[0].Rows[0][0].ToString() != ds1.Tables[0].Rows[0][0].ToString())
                    {
                        throw new Exception($"行号:'[{line.DocLineNo}]',转换前转换后数量不相等");
                    }

                }
            }


            #endregion

        }

    }
}
